/**
 *  StaDiUM - State Diagram Unified Modeler
 *  Copyright (C) 2007  Stefan Bucur, Mihai Balan, Claudiu Tanase
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 *  File Information
 *
 *  Last Modified On: $Date: 2007-06-14 10:24:47 +0000 (Thu, 14 Jun 2007) $
 *  Last Modified By: $Author: stefan.bucur $
 */

package ro.pub.cs.stadium.platform;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class ToolBarManager {
	private CoolBar coolBar;
	
	ToolBar platformToolBar = null;
	ToolBar drawingToolBar = null;
	
	Combo zoomCombo = null;
	
	ToolItem newTI = null;
	ToolItem saveTI = null;
	ToolItem printTI = null;
	
	ToolItem selectTI = null;
	ToolItem panTI = null;
	
	// TODO: put this in a separate plugin
	ToolItem newStateTI = null;
	ToolItem newTransitionTI = null;
	
	Menu newMPopup = null;
	
	
	SelectionListener defaultListener = null;
	
	/**
	 * 
	 * @param name
	 * @return
	 */
	private Image getToolImage(String name) {
		Image result = null;
		try {
			InputStream imageStream = this.getClass().
				getResourceAsStream("icons/" + name);
			
			result = new Image(coolBar.getDisplay(), imageStream);
			
			imageStream.close();
		
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		return result;
	}
	
	private void packOnCoolBar(Control control) {
		Point size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		control.setSize(size);
		
		CoolItem coolItem = new CoolItem(coolBar, SWT.NONE);
		
		Point preferred = coolItem.computeSize(size.x+5, size.y);
		
		
		coolItem.setControl(control);
		coolItem.setSize(preferred);
	}
	
	private ToolBar createPlatformToolBar() {
		
		ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT);
		
		newTI = new ToolItem(toolBar, SWT.DROP_DOWN);
		newTI.setToolTipText("New");
		newTI.setImage(getToolImage("fileNew32.png"));
		newTI.addSelectionListener(defaultListener);
		
		newMPopup = new Menu(coolBar.getShell(), SWT.POP_UP);
		
		saveTI = new ToolItem(toolBar, SWT.PUSH);
		saveTI.setToolTipText("Save");
		saveTI.setImage(getToolImage("fileSave32.png"));
		saveTI.addSelectionListener(defaultListener);
		
		printTI = new ToolItem(toolBar, SWT.PUSH);
		printTI.setToolTipText("Print");
		printTI.setImage(getToolImage("print32.png"));
		printTI.addSelectionListener(defaultListener);
		
		return toolBar;		
	}
	
	private ToolBar createDrawingToolBar() {
		ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT);
		
		selectTI = new ToolItem(toolBar, SWT.RADIO);
		selectTI.setToolTipText("Select");
		selectTI.setImage(getToolImage("select32.png"));
		selectTI.addSelectionListener(defaultListener);
		
		panTI = new ToolItem(toolBar, SWT.RADIO);
		panTI.setToolTipText("Pan");
		panTI.setImage(getToolImage("pan32.png"));
		panTI.addSelectionListener(defaultListener);
		
		// TODO: put this in a separate plugin
		newStateTI = new ToolItem(toolBar, SWT.RADIO);
		newStateTI.setToolTipText("New State");
		newStateTI.setImage(getToolImage("newState32.png"));
		newStateTI.addSelectionListener(defaultListener);
		
		newTransitionTI = new ToolItem(toolBar, SWT.RADIO);
		newTransitionTI.setToolTipText("New Transition");
		newTransitionTI.setImage(getToolImage("newTran32.png"));
		newTransitionTI.addSelectionListener(defaultListener);
		
		
		return toolBar;
	}
	
	private Composite createZoomCombo() {
		Composite result = new Composite(coolBar, SWT.NONE);
		
		RowLayout layout = new RowLayout();
		layout.type = SWT.HORIZONTAL;
		layout.fill = true;
		
		result.setLayout(layout);
		
		Label label = new Label(result, SWT.LEFT);
		label.setText("Zoom:");
		
		Combo combo = new Combo(result, SWT.DROP_DOWN);
		Point comboSize = combo.computeSize(200, SWT.DEFAULT, true);
		combo.setSize(comboSize);
		combo.addSelectionListener(defaultListener);
		
		this.zoomCombo = combo;
		
		result.pack();
		
		return result;
	}
	
	public ToolBarManager(CoolBar coolBar, SelectionListener defaultList) {
		if (coolBar == null)
			throw new NullPointerException("coolBar parameter is null");
		
		this.defaultListener = defaultList;
		
		this.coolBar = coolBar;
	}
	
	public void populateToolBar() {
		platformToolBar = createPlatformToolBar();
		drawingToolBar = createDrawingToolBar();
		
		packOnCoolBar(platformToolBar);
		packOnCoolBar(drawingToolBar);
		packOnCoolBar(createZoomCombo());
	}
	
	/**
	 * 
	 * @return
	 */
	public CoolBar getCoolBar() {
		return coolBar;
	}
	
	/**
	 * 
	 * @return
	 */
	public Combo getZoomCombo() {
		return zoomCombo;
	}
	
	public ToolItem getNewToolItem() {
		return newTI;
	}
	
	public ToolItem getSaveToolItem() {
		return saveTI;
	}
	
	public ToolItem getPrintToolItem() {
		return printTI;
	}
	
	
	public ToolItem getSelectToolItem() {
		return selectTI;
	}
	
	public ToolItem getPanToolItem() {
		return panTI;
	}
	
	// TODO: put this in a separate plugin
	
	public ToolItem getNewStateToolItem() {
		return newStateTI;
	}
	
	public ToolItem getNewTransToolItem() {
		return newTransitionTI;
	}
	
	public ToolBar getPlatformToolbar() {
		return platformToolBar;
	}
	
	public ToolBar getDrawingToolbar() {
		return drawingToolBar;
	}
	
	public Menu getNewPopupMenu() {
		return newMPopup;
	}
}
